var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({},methods,{
	data: {
		isload:0,
		st: 0,
		datalist: [],
		pagenum: 1,
		topbackhide:false,
	},
	_onLoad: function () {
		var that = this;
		if (that.options.st) {
			that.setData({ st: that.options.st })
		}
		var id = 0;
		if (that.options && that.options.id) {
			id = that.options.id
		}
		if (that.options && that.options.scene) {
			var scene = that.options.scene
			var scenes = scene.split('_');
			if(scenes[1]){
				id = parseInt(scenes[1]);
				that.options.id = id
			}
		}
		$.get('business/index',{id:id}, function (res) {
			if(res.isdiy==0){
				res.isload = 1;
				that.setData(res);
				wx.setNavigationBarTitle({
					title: that.data.business.name
				})
				that.setData({ pagenum:1,st:that.data.st,datalist:[],$nomoreData:false})
				that.getDataList();
			}else{
				if(res.status == 2){//付费查看
					$.goto('/pages/wxpay/pagepay?fromPage=index&orderid='+res.orderid+'&pageid='+that.res.id,'redirect');
					return;
				}
				if (res.status == 1) {
					var pagecontent = res.pagecontent
					var _bannerindex = [];
					for(var i in pagecontent){
						_bannerindex[i] = 0;
					}
					that.setData({isdiy:1,title: res.pageinfo.title,sysset: res.sysset,guanggaopic:res.guanggaopic,guanggaourl:res.guanggaourl,pageinfo: res.pageinfo, pagecontent: pagecontent,_bannerindex:_bannerindex})
					wx.setNavigationBarTitle({ title: res.pageinfo.title })
				} else {
					$.alert(res.msg);
				}
			}
    });
	},
	changetab: function (e) {
		var st = e.currentTarget.dataset.st
		this.setData({ pagenum:1,st: st,datalist:[],$nomoreData:false})
		wx.pageScrollTo({
			scrollTop:0,duration:0
		})
		this.getDataList()
	},
	getDataList: function(){
		var that = this
		var pagenum = that.data.pagenum
		var st = that.data.st
		console.log(st)
		$.post('business/getdatalist', {id:that.data.business.id,st:st,pagenum:pagenum}, function (res) {
			var data = res.data
			if(pagenum == 1){
				that.setData({ datalist: data });
				if (data.length == 0){
					$.showEmpty('暂无记录！','/images/empty2.png');
				}
			}else{
				if (data.length == 0){
					that.setData({$nomoreData:true});
				}else{
					var datalist = that.data.datalist
					var newdata = datalist.concat(data)
					that.setData({ datalist: newdata });
				}
			}
		});
	},
	onReachBottom: function () {
		if(this.data.isdiy==0){
			if (!this.data.$dataEmpty.visible && !this.data.$nomoreData){
				this.setData({ pagenum: this.data.pagenum+1 })
				this.getDataList();
			}
		}
	},
  onPageScroll:function(e){
		if(this.data.isdiy==0){
			var that = this;
			var scrollY = e.scrollTop;// 获取垂直滚动的距离，即滚动了多少
			if (scrollY > 200 && !that.data.topbackhide){ //如果滚动距离大于550px则隐藏，否则删除隐藏类
				that.setData({topbackhide:true})
				//that.setData({gotopshow:true})
			}
			if(scrollY < 150 && that.data.topbackhide){
				that.setData({topbackhide:false})
				//that.setData({gotopshow:false})
			}
		}
  },
}))